<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/page/common/commonPage.jsp"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- Head -->
<head>
<meta charset="utf-8" />
<title>用户管理</title>
<!--Page Related styles-->
</head>
<!-- /Head -->
<!-- Body -->
<body>
<!-- 公共header start-->
<%@ include file="/WEB-INF/page/common/header.jsp"%>
<!-- 公共header end-->
<%@ include file="/WEB-INF/page/common/leftSideBar.jsp" %>
<!-- 公共left bar start -->
<!-- 公共left bar end -->

    <div id="main" role="main">
        <div id="ribbon">
            <ol class="breadcrumb">
                <li>首页</li>
                <li>用户管理</li>
            </ol>
        </div>
        <div id="content">
            <div class="row">
                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                    <div class="well">
                        <button class="btn btn-primary" onclick="addUser()">
                            <i class="fa fa-plus"></i> 添加用户
                        </button>
                    </div>
                    <div class="jarviswidget">
                        <div class="widget-body">
                            <table id="userTable" class="table table-striped table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th>用户名</th>
                                        <th>角色</th>
                                        <th>创建时间</th>
                                        <th>最后登录</th>
                                        <th>状态</th>
                                        <th>发文数</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                            <div class="dt-toolbar-footer">
                                <div id="pagination"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="${resourcesUrl}/common/js/jquery-1.8.2.min.js"></script>
    <script src="${resourcesUrl}/common/js/bootstrap/bootstrap.min.js"></script>
    <script type="text/javascript">
        var pageSize = 10;
        var currentPage = 1;

        $(document).ready(function() {
            loadUserList();
        });

        function loadUserList() {
            $.ajax({
                url: '${base}/user/listData',
                type: 'POST',
                data: {
                    pageNo: currentPage,
                    pageSize: pageSize
                },
                success: function(data) {
                    console.log('Response data:', data);
                    if(data.RET_CODE.toLowerCase() === 'success') {
                        var users = data.RET_OBJ;
                        console.log('Users:', users);
                        var html = '';
                        if(users && users.length > 0) {
                            for(var i = 0; i < users.length; i++) {
                                html += '<tr>';
                                html += '<td>' + (users[i].userName || '') + '</td>';
                                html += '<td>' + (users[i].roleValue || '') + '</td>';
                                html += '<td>' + (users[i].buildDate || '') + '</td>';
                                html += '<td>' + (users[i].lastLoginDate || '') + '</td>';
                                html += '<td>' + (users[i].statusValue || '') + '</td>';
                                html += '<td>' + (users[i].count || '0') + '</td>';
                                html += '<td>';
                                html += '<button class="btn btn-xs btn-primary" onclick="editUser(\'' + users[i].userId + '\')"><i class="fa fa-pencil"></i></button> ';
                                if(users[i].status == '1') {
                                    html += '<button class="btn btn-xs btn-warning" onclick="updateStatus(\'' + users[i].userId + '\', \'0\')" title="点击停用"><i class="fa fa-ban"></i></button> ';
                                } else {
                                    html += '<button class="btn btn-xs btn-success" onclick="updateStatus(\'' + users[i].userId + '\', \'1\')" title="点击启用"><i class="fa fa-check"></i></button> ';
                                }
                                html += '<button class="btn btn-xs btn-danger" onclick="deleteUser(\'' + users[i].userId + '\')" title="删除"><i class="fa fa-trash-o"></i></button>';
                                html += '</td>';
                                html += '</tr>';
                            }
                        } else {
                            html = '<tr><td colspan="7" class="text-center">暂无数据</td></tr>';
                        }
                        $('#userTable tbody').html(html);
                    } else {
                        console.log('Error:', data.RET_OBJ);
                        $('#userTable tbody').html('<tr><td colspan="7" class="text-center">加载失败</td></tr>');
                    }
                },
                error: function(xhr, status, error) {
                    console.log('Ajax error:', error);
                    $('#userTable tbody').html('<tr><td colspan="7" class="text-center">加载失败</td></tr>');
                }
            });
        }

        function addUser() {
            window.location.href = '${base}/user/toAdd';
        }

        function editUser(userId) {
            window.location.href = '${base}/user/toEdit?userId=' + userId;
        }

        function updateStatus(userId, status) {
            $.ajax({
                url: '${base}/user/updateStatus',
                type: 'POST',
                data: {
                    userId: userId,
                    status: status
                },
                success: function(data) {
                    if(data.RET_CODE.toLowerCase() === 'success') {
                        alert('操作成功');
                        loadUserList();
                    } else {
                        alert(data.RET_OBJ || '操作失败');
                    }
                }
            });
        }

        function deleteUser(userId) {
            if(confirm('确定要删除该用户吗?')) {
                $.ajax({
                    url: '${base}/user/delete',
                    type: 'POST',
                    data: {
                        userId: userId
                    },
                    success: function(data) {
                        if(data.RET_CODE.toLowerCase() === 'success') {
                            alert('删除成功');
                            loadUserList();
                        } else {
                            alert(data.RET_OBJ || '删除失败');
                        }
                    }
                });
            }
        }
    </script>
    <%@ include file="/WEB-INF/page/common/footer.jsp"%>
</body>
</html> 